package com.investigation.activity.main;
import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.google.android.material.snackbar.Snackbar;
import com.investigation.BR;
import com.investigation.QuestionBean;
import com.investigation.QuestionListBean;
import com.investigation.R;
import com.investigation.TXTManager;
import com.investigation.activity.thanks.ThanksActivity;
import com.investigation.databinding.ActivityMainBinding;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainVM extends AndroidViewModel {
    @SuppressLint("StaticFieldLeak")
    private QuestionAdapter adp;
    private final List<QuestionListBean> data=new ArrayList<>();
    private final List<QuestionBean> questionBeans=new ArrayList<>();//所有问题集合
    public static MutableLiveData<String> nowQuestion=new MutableLiveData<>();
    public static MutableLiveData<String> choosed=new MutableLiveData<>();
    private int questionId=0;
    @SuppressLint("StaticFieldLeak")
    private MainActivity mainActivity;
    public MainVM(@NonNull Application application) {
        super(application);
        nowQuestion.setValue("当前题目未加载");
        choosed.setValue("请选择");
    }
    public void setBinding(ActivityMainBinding binding, MainActivity mainActivity) {
     this.mainActivity=mainActivity;
        adp=new QuestionAdapter(mainActivity,data);
        binding.setAdp(adp);
        setQuestion();
        binding.list.setOnItemClickListener((parent, view, position, id) -> {
            choosed.postValue("您当前已选择:"+data.get(position).question);
            questionBeans.get(questionId).selected=data.get(position).question;
            setNowQuestion();
        });
    }
    public void setQuestion() {
        questionBeans.clear();

        //添加一道题
        List<QuestionListBean>qlb1=new ArrayList<>();
        qlb1.clear();
        qlb1.add(new QuestionListBean("A. はい"));
        qlb1.add(new QuestionListBean("B. いいえ"));
        questionBeans.add(new QuestionBean(qlb1,"この期間中にソフトウェアキーボード利用しましたか？","请选择"));

        //添加第二道题
        List<QuestionListBean>qlb2=new ArrayList<>();
        qlb2.clear();
        qlb2.add(new QuestionListBean("A. 第二个问题的A选项"));
        qlb2.add(new QuestionListBean("B. 第二个问题的B选项"));
        qlb2.add(new QuestionListBean("C. "));
        qlb2.add(new QuestionListBean("D. "));
        qlb2.add(new QuestionListBean("E. "));
        questionBeans.add(new QuestionBean(qlb2,"第二个问题的题目","请选择"));




        //添加完问题
        setNowQuestion();
    }
    private void setNowQuestion() {
        data.clear();
        data.addAll(questionBeans.get(questionId).questionListBeans);
        adp.notifyDataSetChanged();
        nowQuestion.setValue("第"+(questionId+1)+"题，"+questionBeans.get(questionId).title);
        choosed.postValue("您当前已选择:"+questionBeans.get(questionId).selected);
    }
    public void commit() throws JSONException {
        if (questionId+1!=questionBeans.size()||questionBeans.get(questionId).selected.equals("请选择")){
            Toast.makeText(mainActivity, "请先做完", Toast.LENGTH_SHORT).show();
            return;
        }
        questionId=0;
        setNowQuestion();
        String str="";
        for (int i = 0; i < questionBeans.size(); i++) {
            if (i==questionBeans.size()-1){
                str=str+questionBeans.get(i).selected;
            }else{
                str=str+questionBeans.get(i).selected+"/";
            }
        }
        JSONObject obj = new JSONObject();
        obj.put("time",String.valueOf(System.currentTimeMillis()/1000));
        obj.put("data", str.toString());
        JSONArray array = new JSONArray();
        array.put(obj);
        TXTManager txtManager=new TXTManager();
        String log=txtManager.read("config");
        if(log==null){
            txtManager.write("config",array.toString());
        }else{
            JSONArray array1= new JSONArray(log);
            array1.put(obj);
            txtManager.write("config",array1.toString());
        }
        mainActivity.startActivity(new Intent(mainActivity, ThanksActivity.class));
        mainActivity.finish();
    }
    public void last(View view){
        if (questionId==0){
            Snackbar.make(view,"已经是第一题了", Snackbar.LENGTH_SHORT).show();
        }else{
            questionId=questionId-1;
            setNowQuestion();
        }
    }
    public void next(View view){
        if (questionId+1==questionBeans.size()){
            Snackbar.make(view,"已经是最后一题了", Snackbar.LENGTH_SHORT).show();
        }else{
            if (questionBeans.get(questionId).selected.equals("请选择")){
                Snackbar.make(view,"请选择", Snackbar.LENGTH_SHORT).show();
            }else{
                questionId=questionId+1;
                setNowQuestion();
            }
        }
    }
    public static class QuestionAdapter extends ArrayAdapter<QuestionListBean> {
        Context context;
        List<QuestionListBean> data;
        public QuestionAdapter(Context context, List<QuestionListBean> objects){
            super(context, R.layout.item_question,objects);
            this.context=context;
            data=objects;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            ViewDataBinding binding;
            if (convertView == null) {
                binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.item_question, parent, false);
                convertView = binding.getRoot();
                convertView.setTag(binding);
            } else {
                binding = (ViewDataBinding) convertView.getTag();
            }
            binding.setVariable(BR.bean, data.get(position));
            return binding.getRoot();
        }
    }
}



